<template>
  <!-- SelectiveType 申请类型 -->
  <PageContainer>
    <view class="container">
      <Header title="申请类型" />
      <view class="btn-group">
        <template v-for="v in list" :key="v.id">
          <TButton
            class="mb-15"
            :text="v.name"
            :type="'primary'"
            :customStyle="{ width: 'calc(100vw - 60rpx)' }"
            @click="handelSubmit(v)"
          />
        </template>
      </view>
    </view>
  </PageContainer>
</template>

<script lang="ts" setup>
  /**  SelectiveType  申请类型 */
  import PageContainer from '@/components/PageContainer/index.vue';
  import Header from '@/components/Header/index.vue';
  import { applyTemplateList } from '@/api/membership';
  import { redirectTo, onLoad } from '@/router';
  import { ref } from 'vue';
  import { type ApplyTemplateListResult } from '@/api/membership/types';
  import { isLogin } from '@/utils/is';
  import { IsApplyEnum } from '@/enums/membershipEnum';
  import { onShareAppMessage } from '@dcloudio/uni-app';
  import { ShareTypeEnum } from '@/enums/shareEnum';
  import { useAppStoreWithOut } from '@/store/modules/app';
  import { toSelectiveType } from '@/utils/dynamicConfig';
  import TButton from '@/components/TButtonGroup/TButton.vue';
  import { SceneValuesEnum } from '@/enums/sceneValuesEnum';
  import { useAppShare } from '@/hooks/core/useAppShare';

  const { setWxSharePath } = useAppShare();
  const { hasShareImgConfig } = useAppStoreWithOut();
  const list = ref<ApplyTemplateListResult[]>([]);
  function getList() {
    applyTemplateList().then((r) => {
      // 判断只有一个选项,直接进入下一个页面
      if (r.length === 1) {
        handelSubmit(r[0]);
        return;
      }
      list.value = r;
    });
  }

  function handelSubmit(v: ApplyTemplateListResult) {
    if (v.isApply === IsApplyEnum.NOTAPPLY && v.isApplyExplain) {
      redirectTo({ name: 'memberRights', options: { id: v.id } });
    } else {
      redirectTo({
        name: 'membershipInfo',
        options: {
          id: v.id,
        },
      });
    }
  }

  const sceneVals = [
    SceneValuesEnum.SESSION_SINGLEPAGE,
    SceneValuesEnum.CHAT_SINGLEPAGE,
    SceneValuesEnum.SUBSCRIBE_SINGLEPAGE,
    SceneValuesEnum.MENU_SINGLEPAGE,
    SceneValuesEnum.TEMPLATE_SINGLEPAGE,
    SceneValuesEnum.ARTICLE_AD_SINGLEPAGE,
    SceneValuesEnum.CUSTOMER_SERVICE_SINGLEPAGE,
    SceneValuesEnum.CUSTOMER_SERVICE_TEXTLINK_SINGLEPAGE,
    SceneValuesEnum.CHAT_TEXTLINK_SINGLEPAGE,
    SceneValuesEnum.ARTICLE_GOODS_SINGLEPAGE,
    SceneValuesEnum.ARTICLE_SINGLEPAGE,
    SceneValuesEnum.CHAT_MINIPROGRAM_SINGLEPAGE,
    SceneValuesEnum.PROFILE_SINGLEPAGE,
    SceneValuesEnum.ARTICLE_VIDEO_SINGLEPAGE,
    SceneValuesEnum.COMMISSION_GOODS_SINGLEPAGE,
    SceneValuesEnum.SHORT_MOVIE_SINGLEPAGE,
  ];

  onLoad(() => {
    toSelectiveType(
      {
        isRedirectTo: true,
      },
      () => getList(),
      true,
    );
    // const options = uni.getEnterOptionsSync();
    // 通过场景值判断是否由小程序分享进入
    // if (sceneVals.includes(Number(options.scene))) {
    //   toSelectiveType(
    //     {
    //       isRedirectTo: true,
    //     },
    //     () => getList(),
    //     true,
    //   );
    //   return;
    // }
    // isLogin(getList, true);
  });

  onShareAppMessage(() => {
    const membershipShare = hasShareImgConfig(ShareTypeEnum.invite);
    return {
      path: setWxSharePath({ name: 'selectiveType' }),
      imageUrl: membershipShare?.iconUrl,
      title: membershipShare?.desc,
    };
  });
</script>

<style lang="scss" scoped>
  .btn-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 85vh;
  }
</style>
